﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Mechanics.GameEventMechanics {
    import Mechanics.GameMechanicInterfaces.*;
    import Mechanics.*;
    import Classes.*;
    import Classes.util.*;
    import Classes.effects.*;
    import Classes.gates.*;

    public class UpgradeMechanic implements IActionGameMechanic, IMultiPickSupporter, IToolTipModifier {

        protected var m_owner:MechanicMapResource;
        protected var m_config:MechanicConfigData;
        protected var m_gateName:String;
        protected var m_gate:IGate;
        protected var m_gateFactory:GateFactory;
        protected var m_gateTitlePackage:String;
        protected var m_gateTitleKey:String;
        protected var m_gateTitleItemToken:String;

        public function hasOverrideForGameAction(_arg1:String):Boolean{
            return (this.m_owner.isUpgradePossible());
        }
        protected function preUpgradeForGameAction(_arg1:String, _arg2:Array=null):void{
        }
        protected function postUpgradeForGameAction(_arg1:String, _arg2:Array=null):void{
        }
        public function executeOverrideForGameEvent(_arg1:String, _arg2:Array=null):MechanicActionResult{
            var _local3:Boolean;
            var _local4:Boolean;
            var _local5:Boolean;
            if (this.m_owner.isUpgradePossible()){
                if ("true" == this.m_config.params["blockOthers"]){
                    _local4 = true;
                };
                this.preUpgradeForGameAction(_arg1, _arg2);
                if (!this.doDisplayGate()){
                    this.m_owner.upgradeBuildingIfPossible();
                };
                this.postUpgradeForGameAction(_arg1, _arg2);
                _local3 = true;
            };
            return (new MechanicActionResult(_local3, !(_local4), _local5));
        }
        public function doDisplayGate():Boolean{
            var _local2:String;
            var _local3:Object;
            var _local1:Boolean;
            if (this.m_owner.isUpgradePossible()){
                if (this.m_gate){
                    _local2 = null;
                    if (((((this.m_gateTitlePackage) && (this.m_gateTitleKey))) && (this.m_gateTitleItemToken))){
                        _local3 = {};
                        _local3[this.m_gateTitleItemToken] = this.m_owner.getItem().localizedName;
                        _local2 = ZLoc.t(this.m_gateTitlePackage, this.m_gateTitleKey, _local3);
                    };
                    this.m_gate.displayGate("upgrade_", _local2);
                    _local1 = true;
                };
            };
            return (_local1);
        }
        public function initialize(_arg1:IMechanicUser, _arg2:MechanicConfigData):void{
            this.m_gateFactory = new GateFactory();
            this.m_gateFactory.register("inventory", InventoryGate);
            this.m_owner = (_arg1 as MechanicMapResource);
            this.m_config = _arg2;
            this.m_gateName = this.m_config.params["gateName"];
            this.m_gateTitlePackage = this.m_config.params["gateTitlePackage"];
            this.m_gateTitleKey = this.m_config.params["gateTitleKey"];
            this.m_gateTitleItemToken = this.m_config.params["gateTitleItemToken"];
            this.m_gate = this.m_gateFactory.loadGateFromXML(this.m_owner.getItem(), this.m_owner, this.m_gateName, this.onUpgradeSuccess);
        }
        public function isVisitEnabled():Boolean{
            return (false);
        }
        private function onUpgradeSuccess():void{
            var _local1:String;
            if (this.m_owner.isUpgradePossible()){
                this.m_owner.upgradeBuildingIfPossible();
                _local1 = this.m_config.params["effect"];
                if (((_local1) && (!((_local1 == ""))))){
                    this.doUpgradeEffect(_local1);
                };
            };
        }
        private function doUpgradeEffect(_arg1:String):void{
            var effect:* = _arg1;
            var numEffect:* = (int(this.m_config.params["numEffects"])) ? int(this.m_config.params["numEffects"]) : 0;
            var interval:* = (int(this.m_config.params["effectInterval"])) ? int(this.m_config.params["effectInterval"]) : 100;
            MapResourceEffectFactory.createEffect(this.m_owner, EffectType.FIREWORK_BALLOONS);
            var effectsInstantiated:* = 0;
            while (effectsInstantiated <= numEffect) {
                TimerUtil.callLater(function ():void{
                    MapResourceEffectFactory.createEffect(m_owner, EffectType.FIREWORK_BALLOONS);
                }, (effectsInstantiated * interval));
                effectsInstantiated = (effectsInstantiated + 1);
            };
        }
        public function getPick():String{
            return (this.m_config.params["pick"]);
        }
        public function getToolTipStatus():String{
            var _local1:String;
            return (_local1);
        }
        public function getToolTipAction():String{
            var _local1:String;
            if (this.m_owner.isUpgradePossible()){
                _local1 = ZLoc.t("Dialogs", "UpgradeToolTip");
            };
            return (_local1);
        }

    }
}//package Mechanics.GameEventMechanics 
